﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;

namespace SmapleWinformApplication
{
    public partial class Form2 : Form
    {
        ObservableCollection<Details> det;
        sampleDBEntities entities = new sampleDBEntities();
        string Month, Type;
        int Year;
        HomeScreen form;
        int EMPID = 83943;
        public Form2(HomeScreen _form, string type, string month, int year)
        {
            InitializeComponent();

            dataGridView1.Columns[6].Visible = false;

            this.Year = year;
            this.Month = month;
            this.Type = type;
            this.form = _form;
            form.toolStrip1.Enabled = false;

            FetchData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (CbxType.SelectedIndex > 0)
            {
                DateTime dateFrom = dateTimePicker1.Value;
                DateTime dateTo = dateTimePicker2.Value;
                TimeSpan spanDiff = dateFrom - dateTo;
                int dateDiff = Convert.ToInt32(spanDiff.TotalDays);


                DoEntryToTable(dateDiff);
                FetchData();
                ClearEntry();
            }
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            decimal index = Convert.ToDecimal(dataGridView1.SelectedRows[0].Cells[6].Value);
            var tracker = (from d in entities.TIME_TRACKER
                           where d.INDEX == index
                           select d).SingleOrDefault();

            tracker.STATUS = "Delete";
            entities.SaveChanges();

            DateTime dateFrom = dateTimePicker1.Value;
            DateTime dateTo = dateTimePicker2.Value;
            TimeSpan spanDiff = dateFrom - dateTo;

            int dateDiff = Convert.ToInt32(spanDiff.TotalDays);
            int datePositive = -(dateDiff - 1);

            det = new ObservableCollection<Details>();
            double Unit = 0;
            string Month;
            int Year;
            DateTime StartDate;
            DateTime EndDate = default(DateTime);


            StartDate = dateFrom;
            int Mon = StartDate.Month;

            if (checkBox1.Checked == false)
            {
                for (int i = 0; i < datePositive; i++)
                {

                t: if (StartDate.AddDays(i).Month == Mon)
                    {
                        Unit = Unit + 1;
                    }
                    else
                    {
                        EndDate = StartDate.AddDays(i - 1);
                        det.Add(new Details(Unit, StartDate.ToString("MMM"), StartDate.Year, StartDate, EndDate));

                        Mon = StartDate.AddDays(i).Month;
                        Unit = 0;
                        goto t;
                    }
                }

                if (Unit > 0 && EndDate != default(DateTime))
                {
                    det.Add(new Details(Unit, EndDate.AddDays(1).ToString("MMM"), EndDate.Year, EndDate.AddDays(1), EndDate.AddDays(Convert.ToInt32(Unit))));
                }
                else
                {
                    det.Add(new Details(Unit, StartDate.ToString("MMM"), StartDate.Year, StartDate, StartDate.AddDays(Convert.ToInt32(Unit))));
                }
            }
            else
            {
                DateTime date = dateTimePicker1.Value;
                Unit = 0.5;
                det.Add(new Details(Unit, date.ToString("MMM"), date.Year, date, date));

            }


            foreach (Details item in det)
            {
                TIME_TRACKER newTracker = new TIME_TRACKER();

                var query = (from d in entities.TIME_TRACKER
                             select d).Count();

                newTracker.INDEX = query + 1;
                newTracker.EMPID = 83943;
                newTracker.YEAR = item.Year;
                newTracker.MONTH = item.Month;
                newTracker.TYPE = CbxType.SelectedItem.ToString();
                newTracker.UNIT = Convert.ToDecimal(item.Unit);
                newTracker.FROMDATE = item.StartDate;
                newTracker.TODATE = item.EndDate;
                newTracker.STATUS = "Edit";

                entities.AddToTIME_TRACKER(newTracker);
                entities.SaveChanges();
            }

            FetchData();
            ClearEntry();
        }

        public void ClearEntry()
        {
            CbxType.SelectedIndex = 0;
            checkBox1.Checked = false;
            dateTimePicker1.Value = DateTime.Now;
            dateTimePicker2.Value = DateTime.Now;
        }


        public void DoEntryToTable(int period)
        {
            decimal TempPeriod = 0, ELCount = 0, MLCount = 0, SPLCount = 0;

            var query = from d in entities.TIME_TRACKER
                        where d.EMPID == EMPID && d.STATUS != "Delete"
                        select d;

            foreach (var item in query)
            {
                if (item.TYPE == "EL")
                    ELCount = ELCount + Convert.ToDecimal(item.UNIT);
                else if (item.TYPE == "ML")
                    MLCount = MLCount + Convert.ToDecimal(item.UNIT);
                else if (item.TYPE == "SPL")
                    SPLCount = SPLCount + Convert.ToDecimal(item.UNIT);
            }

            var query1 = from d in entities.TIME_TRACKER
                         select d;

            int count = query1.Count();
            TIME_TRACKER tracker = new TIME_TRACKER();
            tracker.INDEX = count + 1;
            tracker.EMPID = 83943;
            tracker.YEAR = DateTime.Now.Year;
            DateTime date = DateTime.Now;
            tracker.MONTH = Month;

            if (CbxType.SelectedItem.ToString() == "ML" && MLCount < 10)
            {
                tracker.TYPE = "ML";
            }
            else if (CbxType.SelectedItem.ToString() == "SPL" && SPLCount < 90)
            {
                tracker.TYPE = "SPL";
            }

            else if (CbxType.SelectedItem.ToString() == "EL" && ELCount < 30)
            {
                tracker.TYPE = "EL";
            }
            else
            {
                tracker.TYPE = "LOP";
            }
            if (checkBox1.Checked)
            {
                tracker.UNIT = Convert.ToDecimal(0.5);
            }
            else
            {
                tracker.UNIT = Convert.ToDecimal(-(period - 1));
            }
            tracker.FROMDATE = dateTimePicker1.Value;
            tracker.TODATE = dateTimePicker2.Value;
            tracker.STATUS = "New";

            entities.AddToTIME_TRACKER(tracker);
            entities.SaveChanges();
        }

        public void FetchData()
        {
            var query = from d in entities.TIME_TRACKER
                        where d.TYPE == Type && d.MONTH == Month && d.YEAR == Year && d.STATUS != "Delete"
                        select d;

            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.Rows.Clear();
            }

            foreach (var item in query)
            {
                dataGridView1.Rows.Add(item.MONTH, item.YEAR, item.TYPE, item.UNIT, item.FROMDATE, item.TODATE, item.INDEX);
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                CbxType.SelectedItem = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                dateTimePicker1.Value = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells[4].Value.ToString());
                dateTimePicker2.Value = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells[5].Value.ToString());
                textBox1.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1(form);
            form1.MdiParent = form;
            this.Close();
            form1.Show();   
        }

        private void checkBox1_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
                dateTimePicker2.Visible = false;
            else
                dateTimePicker2.Visible = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                decimal index = Convert.ToDecimal(textBox1.Text);
                var query = (from d in entities.TIME_TRACKER
                             where d.INDEX == index
                             select d).SingleOrDefault();

                query.STATUS = "Delete";
                entities.SaveChanges();

                FetchData();
                ClearEntry();
            }
        }
    }
    public class Details
    {
        public double Unit;
        public string Month;
        public int Year;
        public DateTime StartDate;
        public DateTime EndDate;

        public Details(double unit, string month, int year, DateTime startDate, DateTime endDate)
        {
            Unit = unit;
            Month = month;
            Year = year;
            StartDate = startDate;
            EndDate = endDate;
        }
    }
}
